package com.zyf.kafka.consumer;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Malegod_xiaofei
 * @create 2024-08-20-21:20
 */
public class KafkaConsumerGroup3Test {
    public static void main(String[] args) {

        // TODO 创建配置对象
        Map<String, Object> consumerConfig = new HashMap();
        consumerConfig.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        consumerConfig.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        consumerConfig.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        consumerConfig.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        consumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, "zyf");
        // consumerConfig.put(ConsumerConfig.GROUP_INSTANCE_ID_CONFIG, "ccc");
        // consumerConfig.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, RoundRobinAssignor.class.getName());
        // consumerConfig.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, RangeAssignor.class.getName());

        // TODO 创建消费者对象
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(consumerConfig);

        // TODO 订阅主题
        consumer.subscribe(Collections.singletonList("test1"));

        // TODO 从 kafka 的主题中获取数据
        //      消费者从 kafka 中拉取数据
        while (true) {
            final ConsumerRecords<String, String> datas = consumer.poll(Duration.ofMillis(100));

            for (ConsumerRecord<String, String> data : datas) {
                System.out.println(data.partition());
            }
        }

        // LEO : log End Offset = size + 1 = 0,1,2,3   [4]

        // 关闭消费者对象
        // consumer.close();
    }
}
